package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.zhenhuan.firstProcess.UserZhenHuanFirstProcessData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.GameZhenHuanFinishPartyData;
import com.yanqu.road.entity.activity.zhenhuan.secondProcess.UserZhenHuanSecondProcessData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class UserZhenHuanSecondDaoImpl extends TempDataObjectDao<UserZhenHuanSecondProcessData> {

    public static String getUserZhenHuanSecondProcessDataSql = "select * from `t_u_zhenhuan_second` where `activityId`=? and `userId`=?";

    public UserZhenHuanSecondProcessData getUserZhenHuanSecondProcessData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserZhenHuanSecondProcessDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserZhenHuanSecondProcessDataSql = "update `t_u_zhenhuan_second` set power=?,powerRecoverTime=?,isWorship=?,level=?,loveDegree=?,prestigeDegree=?,goingOutAmount=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserZhenHuanSecondProcessData userZhenHuanSecondProcessData) {
        userZhenHuanSecondProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanSecondProcessData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getLoveDegree()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getPrestigeDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getGoingOutAmount()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getUserId()));
        if (getDb().executeNoneQuery(updateUserZhenHuanSecondProcessDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserZhenHuanSecondProcessDataSql = "insert into `t_u_zhenhuan_second`(activityId,userId,power,powerRecoverTime,isWorship,level,loveDegree,prestigeDegree,goingOutAmount) values (?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserZhenHuanSecondProcessData userZhenHuanSecondProcessData) {
        userZhenHuanSecondProcessData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getPowerRecoverTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userZhenHuanSecondProcessData.isWorship()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getLoveDegree()));
        para.put(i++, new DbParameter(Types.BIGINT, userZhenHuanSecondProcessData.getPrestigeDegree()));
        para.put(i++, new DbParameter(Types.INTEGER, userZhenHuanSecondProcessData.getGoingOutAmount()));
        if (getDb().executeNoneQuery(insertUserZhenHuanSecondProcessDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserZhenHuanSecondProcessData parseData(ResultSet rs) throws SQLException {
        UserZhenHuanSecondProcessData userZhenHuanSecondProcessData = new UserZhenHuanSecondProcessData();
        userZhenHuanSecondProcessData.setActivityId(rs.getInt("activityId"));
        userZhenHuanSecondProcessData.setUserId(rs.getLong("userId"));
        userZhenHuanSecondProcessData.setPower(rs.getInt("power"));
        userZhenHuanSecondProcessData.setPowerRecoverTime(rs.getLong("powerRecoverTime"));
        userZhenHuanSecondProcessData.setWorship(rs.getBoolean("isWorship"));
        userZhenHuanSecondProcessData.setLevel(rs.getInt("level"));
        userZhenHuanSecondProcessData.setLoveDegree(rs.getLong("loveDegree"));
        userZhenHuanSecondProcessData.setPrestigeDegree(rs.getLong("prestigeDegree"));
        userZhenHuanSecondProcessData.setGoingOutAmount(rs.getInt("goingOutAmount"));

        return userZhenHuanSecondProcessData;
    }
}
